package jubao.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.whu.zj.MyApplication;
import com.whu.zj.R;
import com.whu.zj.UIHandler;
import com.whu.zj.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jubao.dto.GrassrootsOpinion;
import utils.TempUtil;
import utils.TimeUtil;

/**
 * Created by Administrator on 2016/12/26.
 */
public class UpdateJuBao extends UIHandler.BaseActivity {

    GrassrootsOpinion grassrootsOpinion;
    EditText et_time_deal;
    EditText et_people_deal;
    EditText et_content;

    Button btn_commit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        grassrootsOpinion = (GrassrootsOpinion) TempUtil.getObj();
        setContentView(R.layout.layout_updatejubao);
        initview();
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (view == btn_commit)
            {
                if (grassrootsOpinion.getFlag().equals("1"))
                {
                    Toast.makeText(MyApplication.share(),"已经处理过了",Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!checkd(et_time_deal.getText().toString())) {
                    return;
                }
                commitDeal(et_time_deal.getText().toString(),et_people_deal.getText().toString());
            }
        }
    };

    private boolean checkd(String time) {
        if (!time.matches("[0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}")) {
            Toast.makeText(MyApplication.share(), "时间格式不正确", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    private void commitDeal(final String time, final String personName) {
        String IP = MyApplication.share().getResources().getString(R.string.IP_zj);
        String url ="http://"+IP+"/UpdateGrassrootOpinion";
        StringRequest stringRequest = new StringRequest(Request.Method.POST,url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (response.equals("\"success\""))
                        {
                            Toast.makeText(MyApplication.share(),"处理成功",Toast.LENGTH_SHORT).show();
                            UpdateJuBao.this.finish();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MyApplication.share(),"获取失败",Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                map.put("crowdName",grassrootsOpinion.getCrowdName());
                map.put("crowdTime",grassrootsOpinion.getCrowdTime());
                map.put("disposeName",personName);
                map.put("disposeTime",time);
                return  map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data,"utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    private void initview() {
        et_time_deal = (EditText) findViewById(R.id.id_time_deal);
        et_time_deal.setText(TimeUtil.currentTimeSP2());

        et_people_deal = (EditText) findViewById(R.id.id_et_people_deal);
        et_content = (EditText) findViewById(R.id.id_et_content_deal);
        et_content.setText(grassrootsOpinion.getCrowdContext());

        btn_commit = (Button) findViewById(R.id.id_btn_addJubao);
        btn_commit.setOnClickListener(onClickListener);
    }
}
